home *** CD-ROM | disk | FTP | other *** search
- /* 4567890123456789012345678901234567890123456789012345678901234567 */
- /*
- * File: main.c
- * Author: Mark H. Linton
- * Creation Date: 2/4/95
- * Modification Date:
- * Description: A sample procedural shell for a typical
- * Macintosh appplication.
- */
- #include <Dialogs.h>
- #include <Memory.h>
- #include <Errors.h>
- #include <AppleEvents.h>
- #include <Desk.h>
- #include <ToolUtils.h>
- #include <StandardFile.h>
- #include <Resources.h>
- #include <Gestalt.h>
-
- #include "main.h"
- #include "Document.h"
-
- #include "Toolbox.h"
- #include "Splash.h"
- #include "AERequired.h"
- #include "HasRequiredFeatures.h"
- #include "About.h"
- #include "ProcessInfo.h"
- #include "EvenMoreFiles.h"
- #include "SaveSafely.h"
-
- #include "DSDocumentList.h"
- #include "DSDocumentMenu.h"
- #include "DSSyncWindowWithFile.h"
- #include "DSPopUpNavigation.h"
-
- #include "ResourceDefinitions.h"
-
- enum {
- iAbout = 1,
- iNew = 1,
- iOpen = 2,
- iClose = 4,
- iSave = 5,
- iSaveAs = 6,
- iRevert = 7,
- iPageSetup = 9,
- iPrint = 10,
- iQuit = 12
- };
-
- static OSErr InstallMenuBar(void);
- static OSErr AdjustMenus(void);
- static OSErr AdjustFileMenu(void);
- static OSErr EventLoop(void);
- static OSErr DoMouseDown(EventRecord *anEvent);
- static OSErr DoCommand(long aMenuSelection);
- static OSErr DoFileCommand(short anItemNumber);
- static long SleepTime(void);
-
- struct ApplicationGlobals {
- Boolean done;
- } g; /* do not clutter global name space, Re: qd.thePort */
-
- #define kFrontSleep (10) /* 1/6 of a second */
- #define kBackSleep (600) /* 10 seconds */
-
- #define kDontSave (3)
-
- int main(void) {
- OSErr theError = noErr;
-
- theError = InitToolbox();
- if ((theError == noErr) && (HasRequiredFeatures())) {
- if (theError == noErr) {
- Splash(rSplashScreen);
- theError = DSInitDocumentList();
- }
- if (theError == noErr) {
- theError = InstallMenuBar();
- }
- if (theError == noErr) {
- theError = InstallRequiredAppleEvents();
- }
- if (theError == noErr) {
- UnSplash(true);
- InitCursor();
- theError = EventLoop();
- }
- } else if (theError == noErr) {
- (void)Alert(rMissingFeatureAlert, nil);
- }
- if (theError != noErr) ReportError(theError);
- return theError;
- }
-
- OSErr InstallMenuBar(void) {
- Handle theMenuBar = nil;
- OSErr theError = noErr;
- MenuHandle theAppleMenu = nil;
-
- theMenuBar = GetNewMBar(rMenuBar);
- if (theMenuBar != nil) {
- SetMenuBar(theMenuBar);
- DisposeHandle(theMenuBar);
- DSInitDocumentMenu(mDocument);
- theAppleMenu = GetMenuHandle(mApple);
- if (theAppleMenu != nil) {
- AppendResMenu(theAppleMenu, 'DRVR');
- theError = AdjustMenus();
- } else {
- theError = memFullErr;
- }
- }
- if (theError == noErr) {
- DrawMenuBar();
- }
- return theError;
- }
-
- OSErr AdjustMenus(void) {
- OSErr theError = noErr;
-
- theError = AdjustFileMenu();
- DSAdjustDocumentMenu();
- return theError;
- }
-
- OSErr AdjustFileMenu(void) {
- #if STRICT_WINDOWS
- WindowRef theWindow;
- #else
- WindowPtr theWindow;
- #endif
- MenuHandle theMenu;
- short theFRefNum;
- OSErr theError = noErr;
-
- theMenu = GetMenuHandle(mFile);
- if (theMenu != nil) {
- EnableItem(theMenu, iQuit);
- EnableItem(theMenu, iNew);
- EnableItem(theMenu, iOpen);
- theWindow = FrontWindow();
- if (!DSInDocumentList(theWindow)) {
- DisableItem(theMenu, iSaveAs);
- DisableItem(theMenu, iClose);
- DisableItem(theMenu, iSave);
- } else {
- EnableItem(theMenu, iSaveAs);
- EnableItem(theMenu, iClose);
- theError =
- DSGetWindowDFRefNum(FrontWindow(), &theFRefNum);
- if ((theError == noErr) && (theFRefNum != 0) &&
- DSIsWindowDirty(FrontWindow())) {
- EnableItem(theMenu, iSave);
- EnableItem(theMenu, iRevert);
- } else {
- DisableItem(theMenu, iSave);
- DisableItem(theMenu, iRevert);
- }
- }
- } else {
- theError = menuPrgErr;
- }
- return theError;
- }
-
- OSErr EventLoop(void) {
- RgnHandle theCursorRegion = NewRgn();
- EventRecord theEvent;
- OSErr theError = noErr;
- Boolean gotEvent;
-
- g.done = false;
- while (!g.done && (theError == noErr)) {
- gotEvent = WaitNextEvent(everyEvent, &theEvent, SleepTime(),
- theCursorRegion);
- if (gotEvent) {
- CheckGrowZone();
- theError = DoEvent(&theEvent);
- }
- if (theError == noErr) {
- theError = DSSyncWindowsWithFiles(kDontForceSynchronization);
- }
- }
- DisposeRgn(theCursorRegion);
- return theError;
- }
-
- pascal OSErr DoEvent(EventRecord *anEvent) {
- OSErr theError = noErr;
-
- switch (anEvent->what) {
- case keyDown:
- case autoKey:
- if ((anEvent->modifiers & cmdKey) != 0) {
- AdjustMenus();
- DoCommand(MenuKey(anEvent->message & charCodeMask));
- } else {
- /* DoKeyDown(anEvent); */
- }
- break;
- case mouseDown:
- theError = DoMouseDown(anEvent);
- break;
- case activateEvt:
- #if STRICT_WINDOWS
- if (DSInDocumentList((WindowRef)anEvent->message)) {
- DrawGrowIcon((WindowRef)anEvent->message);
- }
- #else
- if (DSInDocumentList((WindowPtr)anEvent->message)) {
- DrawGrowIcon((WindowPtr)anEvent->message);
- }
- #endif
- break;
- case osEvt:
- if (((anEvent->message >> 24) & 0xff) == suspendResumeMessage) {
- if (DSInDocumentList(FrontWindow())) {
- DrawGrowIcon(FrontWindow());
- }
- theError = DSSyncWindowsWithFiles(kForceSynchronization);
- }
- break;
- case updateEvt:
- #if STRICT_WINDOWS
- BeginUpdate((WindowRef)anEvent->message);
- DoDrawDocument((WindowRef)anEvent->message);
- DrawGrowIcon((WindowRef)anEvent->message);
- EndUpdate((WindowRef)anEvent->message);
- #else
- BeginUpdate((WindowPtr)anEvent->message);
- DoDrawDocument((WindowPtr)anEvent->message);
- DrawGrowIcon((WindowPtr)anEvent->message);
- EndUpdate((WindowPtr)anEvent->message);
- #endif
- break;
- case diskEvt:
- DoDiskEvent(anEvent->message);
- break;
- case kHighLevelEvent:
- theError = AEProcessAppleEvent(anEvent);
- break;
- }
- return theError;
- }
-
- OSErr DoMouseDown(EventRecord *anEvent) {
- #if STRICT_WINDOWS
- WindowRef theWindow;
- #else
- WindowPtr theWindow;
- #endif
- short theWindowPart = DSFindWindow(anEvent->where, &theWindow);
- RgnHandle theGrayRegion;
- OSErr theError = noErr;
-
- switch (theWindowPart) {
- case inMenuBar:
- theError = AdjustMenus();
- if (theError == noErr) {
- theError = DoCommand(MenuSelect(anEvent->where));
- }
- break;
- case inSysWindow:
- #if STRICT_WINDOWS
- SystemClick(anEvent, (WindowPtr)GetWindowPort(theWindow));
- #else
- SystemClick(anEvent, theWindow);
- #endif
- break;
- case inDrag:
- theGrayRegion = GetGrayRgn();
- DragWindow(theWindow, anEvent->where,
- &(**theGrayRegion).rgnBBox);
- break;
- case inContent:
- if (theWindow != FrontWindow()) {
- SelectWindow(theWindow);
- } else {
- DoContentClick(theWindow, anEvent);
- }
- break;
- case inGrow:
- if (DSInDocumentList(theWindow)) {
- Rect theLimitRect = { 68, 110, 32767, 32767 };
- long theGrowSize =
- GrowWindow(theWindow, anEvent->where, &theLimitRect);
-
- if (theGrowSize != 0) {
- GrafPtr theCurrentPort;
-
- GetPort(&theCurrentPort);
- #if STRICT_WINDOWS
- SetPortWindowPort(theWindow);
- EraseRect(&GetWindowPort(theWindow)->portRect);
- #else
- SetPort(theWindow);
- EraseRect(&theWindow->portRect);
- #endif
- SizeWindow(theWindow, LoWord(theGrowSize),
- HiWord(theGrowSize), true);
- #if STRICT_WINDOWS
- InvalRect(&GetWindowPort(theWindow)->portRect);
- #else
- InvalRect(&theWindow->portRect);
- #endif
- SetPort(theCurrentPort);
- }
- }
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, anEvent->where)) {
- theError = DoCloseCommand(theWindow, true);
- if ((theError == noErr) &&
- ((anEvent->modifiers & optionKey) != 0)) {
- for (theWindow = DSFirstWindow();
- (theWindow != nil) && (theError == noErr);
- theWindow = DSFirstWindow()) {
- theError = DoCloseCommand(theWindow, true);
- }
- }
- if (theError == userCanceledErr) theError = noErr;
- }
- break;
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(theWindow, anEvent->where, theWindowPart)) {
- GrafPtr theCurrentPort;
-
- GetPort(&theCurrentPort);
- #if STRICT_WINDOWS
- SetPortWindowPort(theWindow);
- EraseRect(&GetWindowPort(theWindow)->portRect);
- #else
- SetPort(theWindow);
- EraseRect(&theWindow->portRect);
- #endif
- ZoomWindow(theWindow, theWindowPart,
- (theWindow == FrontWindow()));
- SetPort(theCurrentPort);
- }
- break;
- }
- return theError;
- }
-
- OSErr DoCommand(long aMenuSelection) {
- OSErr theError = noErr;
-
- switch (HiWord(aMenuSelection)) {
- case mApple:
- if (LoWord(aMenuSelection) == iAbout) {
- DoAbout(rAbout);
- } else {
- Str255 theItemName;
-
- GetMenuItemText(GetMenuHandle(mApple),
- LoWord(aMenuSelection), theItemName);
- (void)OpenDeskAcc(theItemName);
- }
- break;
- case mFile:
- theError = DoFileCommand(LoWord(aMenuSelection));
- break;
- case mDocument:
- DSDoDocumentCommand(LoWord(aMenuSelection));
- break;
- }
- HiliteMenu(0);
-
- return theError;
- }
-
- OSErr DoFileCommand(short anItemNumber) {
- OSErr theError = noErr;
- StandardFileReply theReply;
- SFTypeList theTypeList = { '.oO~' };
-
- switch (anItemNumber) {
- case iNew:
- theError = DoNewCommand();
- break;
- case iOpen:
- StandardGetFile(nil, 1, theTypeList, &theReply);
- if (theReply.sfGood) {
- theError = DoOpenCommand(&theReply.sfFile);
- }
- break;
- case iClose:
- theError = DoCloseCommand(FrontWindow(), true);
- break;
- case iSave:
- theError = DoSave(FrontWindow());
- break;
- case iSaveAs:
- theError = DoSaveAsCommand();
- break;
- case iRevert:
- theError = DoRevertCommand(FrontWindow());
- break;
- case iQuit:
- theError = DoQuit();
- break;
- }
- if (theError == userCanceledErr) {
- /*
- * If the user cancelled, whatever action was going to
- * occur has already been handled.
- */
- theError = noErr;
- }
- return theError;
- }
-
- pascal OSErr DoNewCommand(void) {
- #if STRICT_WINDOWS
- WindowRef theWindow;
- #else
- WindowPtr theWindow;
- #endif
- OSErr theError = noErr;
- long theVersion;
-
- #if 0
- if (HasFeature(gestaltQuickdrawFeatures, gestaltHasColor)) {
- /*
- * You might think that this was the correct way to check for the
- * presence of Color QuickDraw--and hence the ability to use routines
- * like GetNewCWindow instead of GetNewWindow-- but, you would be wrong.
- *
- * This call returns true on a Macintosh SE running System 7.5, but the
- * GetNewCWindow trap is, in fact, not available on that machine.
- *
- * There is a note in "Inside Macintosh: Imaging with QuickDraw," in the
- * section "Using Color QuickDraw," where it says:
- *
- * "When testing for the existence of Color QuickDraw, your
- * application should test the response to the
- * gestaltQuickDrawVersion selector (rather than test for the result
- * gestaltHasColor, which is unreliable, from the
- * gestaltQuickDrawFeatures selector)."
- *
- * Heed this advice.
- */
- #else
- /*
- * Check the QuickDraw version instead. Like this:
- */
- theError = Gestalt(gestaltQuickdrawVersion, &theVersion);
- if ((theError == noErr) && (theVersion > gestaltOriginalQD)) {
- #endif
- theWindow = GetNewCWindow(rDocWindow, nil, kInFront);
- } else {
- if (theError != noErr) {
- /*
- * We will assume that if an error occurred, it was just a problem
- * with Gestalt--that the program is OK, but we probably do not
- * have Color QuickDraw. We could set a breakpoint here to
- * investigate, if we really wanted to (i.e. - THIS SHOULD NEVER
- * HAPPEN =^).
- */
- theError = noErr;
- }
- theWindow = GetNewWindow(rDocWindow, nil, kInFront);
- }
- if (theWindow != nil) {
- theError = DSAddWindow(theWindow);
- if (theError == noErr) {
- theError = DSSetCreatorAndFileType(theWindow,
- GetSignature(), GetSignature());
- }
- if (theError == noErr) {
- theError = DSSetWindowDFRefNum(theWindow, 0);
- }
- if (theError == noErr) {
- ShowWindow(theWindow);
- DSAdjustDocumentMenu();
- }
- } else {
- theError = memFullErr;
- }
- return theError;
- }
-
- pascal OSErr DoOpenCommand(FSSpec *aFile) {
- OSErr theError;
- #if STRICT_WINDOWS
- WindowRef theWindow;
- #else
- WindowPtr theWindow;
- #endif
- short theFRefNum;
-
- if (DSFileInDocumentList(aFile, &theWindow)) {
- SelectWindow(theWindow);
- } else {
- theError = DoNewCommand();
- if (theError == noErr) {
- theError = FSpOpenDF(aFile, fsCurPerm, &theFRefNum);
- if (theError == permErr) {
- ParamText(aFile->name, kNullString, kNullString, kNullString);
- (void)StopAlert(rPermErrAlert, nil);
- (void)DoCloseCommand(FrontWindow(), false);
- }
- }
- if (theError == noErr) {
- theWindow = FrontWindow();
- theError = DSSetWindowDFRefNum(theWindow, theFRefNum);
- }
- if (theError == noErr) {
- theError = DoReadDocument(theWindow);
- }
- if (theError == noErr) {
- SetWTitle(theWindow, aFile->name);
- }
- }
- DSAdjustDocumentMenu();
- return theError;
- }
-
- #if STRICT_WINDOWS
- OSErr DoCloseCommand(WindowRef aWindow, Boolean notify) {
- #else
- OSErr DoCloseCommand(WindowPtr aWindow, Boolean notify) {
- #endif
- short theFRefNum;
- OSErr theError = noErr;
- short theResponse;
- Str255 theTitle;
- Boolean proceed;
-
- if (DSInDocumentList(aWindow)) {
- if (DSIsWindowDirty(aWindow) && notify) {
- GetWTitle(aWindow, theTitle);
- ParamText(theTitle, kNullString, kNullString, kNullString);
- theResponse = StopAlert(rCloseDirtyAlert, nil);
- switch (theResponse) {
- case ok: /* Save */
- theError = DSGetWindowDFRefNum(aWindow, &theFRefNum);
- proceed = false;
- if (theError == noErr) {
- if (theFRefNum == 0) {
- theError = DoSaveAsCommand();
- } else {
- theError = DoSave(aWindow);
- }
- if (theError != noErr) {
- proceed = false;
- } else {
- proceed = true;
- }
- }
- break;
- case cancel:
- proceed = false;
- theError = userCanceledErr;
- break;
- case kDontSave:
- proceed = true;
- break;
- }
- } else {
- proceed = true;
- }
- if ((theError == noErr) && (proceed)) {
- theError = DSGetWindowDFRefNum(aWindow, &theFRefNum);
- if ((theError == noErr) && (theFRefNum != 0)) {
- theError = FSClose(theFRefNum);
- }
- if (theError == noErr) {
- theError = DSGetWindowRFRefNum(aWindow, &theFRefNum);
- if ((theError == noErr) && (theFRefNum != 0)) {
- CloseResFile(theFRefNum);
- theError = ResError();
- }
- }
- if (theError == noErr) {
- theError = DoRemoveDocumentData(aWindow);
- }
- if (theError == noErr) {
- theError = DSRemoveWindow(aWindow);
- }
- if (theError == noErr) {
- DisposeWindow(aWindow);
- }
- }
- DSAdjustDocumentMenu();
- }
- return theError;
- }
-
- #if STRICT_WINDOWS
- OSErr DoRevertCommand(WindowRef aWindow) {
- #else
- OSErr DoRevertCommand(WindowPtr aWindow) {
- #endif
- short theFRefNum;
- OSErr theError = noErr;
- short theResponse;
- Str255 theTitle;
- FSSpec theFile;
-
- if (DSInDocumentList(aWindow)) {
- GetWTitle(aWindow, theTitle);
- ParamText(theTitle, kNullString, kNullString, kNullString);
- theResponse = StopAlert(rRevertAlert, nil);
- if (theResponse == ok) {
- theError = DSGetWindowDFRefNum(aWindow, &theFRefNum);
- if ((theError == noErr) && (theFRefNum != 0)) {
- theError = GetFileSpec(theFRefNum, &theFile);
- if (theError == noErr) {
- theError = FSClose(theFRefNum);
- }
- if (theError == noErr) {
- theError = DoRemoveDocumentData(aWindow);
- }
- if (theError == noErr) {
- theError = DSRemoveWindow(aWindow);
- }
- if (theError == noErr) {
- DisposeWindow(aWindow);
- }
- if (theError == noErr) {
- theError = DoOpenCommand(&theFile);
- }
- }
- }
- }
- return theError;
- }
-
- #if STRICT_WINDOWS
- OSErr DoSave(WindowRef aWindow) {
- #else
- OSErr DoSave(WindowPtr aWindow) {
- #endif
- OSErr theError = noErr;
- short theDFRefNum;
-
- theError = DSGetWindowDFRefNum(aWindow, &theDFRefNum);
- if (theError == noErr) {
- theError = SaveSafely(aWindow, nil, 0, DoWriteDocument, nil);
- }
- if (theError == noErr) {
- theError = DSWindowClean(aWindow);
- }
-
- return theError;
- }
-
- OSErr DoSaveAsCommand(void) {
- #if STRICT_WINDOWS
- WindowRef theWindow;
- #else
- WindowPtr theWindow;
- #endif
- StandardFileReply theReply;
- OSErr theError = noErr;
- Str255 prompt = "\pSave As:", defaultName = "\pUntitled";
- short theFRefNum;
- OSType theCreator, theFileType;
-
- theWindow = FrontWindow();
- if (DSInDocumentList(theWindow)) {
- theError = DSGetWindowDFRefNum(theWindow, &theFRefNum);
- if ((theError == noErr) && (theFRefNum != 0)) {
- GetWTitle(theWindow, defaultName);
- }
- StandardPutFile(prompt, defaultName, &theReply);
- if (theReply.sfGood) {
- if ((theError == noErr) && (theFRefNum != 0)) {
- theError = FSClose(theFRefNum);
- }
- if ((theError == noErr) && (theReply.sfReplacing)) {
- theError = FSpDelete(&theReply.sfFile);
- }
- if (theError == noErr) {
- theError = DSGetCreatorAndFileType(theWindow,
- &theCreator, &theFileType);
- }
- if (theError == noErr) {
- theError = FSpCreate(&theReply.sfFile, theCreator,
- theFileType, theReply.sfScript);
- }
- if (theError == noErr) {
- theError = FSpOpenDF(&theReply.sfFile, fsCurPerm,
- &theFRefNum);
- }
- if (theError == noErr) {
- theError = DSSetWindowDFRefNum(theWindow, theFRefNum);
- }
- if (theError == noErr) {
- theError = DoWriteDocument(theWindow, theFRefNum);
- }
- if (theError == noErr) {
- SetWTitle(theWindow, theReply.sfFile.name);
- theError = DSWindowClean(theWindow);
- }
- } else {
- theError = userCanceledErr;
- }
- } else {
- theError = paramErr;
- }
- DSAdjustDocumentMenu();
-
- return theError;
- }
-
- pascal OSErr DoQuit(void) {
- OSErr theError = noErr;
- #if STRICT_WINDOWS
- WindowRef theWindow, theNextWindow;
- #else
- WindowPtr theWindow, theNextWindow;
- #endif
-
- theWindow = DSFirstWindow();
- while ((theWindow != nil) && (theError != userCanceledErr)) {
- theNextWindow = DSNextWindow(theWindow);
- theError = DoCloseCommand(theWindow, true);
- theWindow = theNextWindow;
- }
- if (theError == userCanceledErr) {
- theError = noErr;
- } else {
- g.done = true;
- }
- return theError;
- }
-
- long SleepTime(void) {
- OSErr theError;
- long theSleepTime;
- Boolean inBackground;
-
- theError = InBackground(&inBackground);
- if ((theError == noErr) && (inBackground)) {
- theSleepTime = kBackSleep;
- } else {
- theSleepTime = kFrontSleep;
- }
- return theSleepTime;
- }
-